from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
import os
import json

# Create your views here.
def resume(request):
    return render(request,'test.html')
    # return HttpResponse('helloworld')
    # return HttpResponseRedirect('Resume.html') 这种不对
    # return HttpResponseRedirect('/login/')

def upload_resume(request):
    random_str = request.GET['random_str']  # url 中带的字段
    print(random_str)
    myFile = request.FILES.get('file')
    print(myFile)
    file_name = random_str + '.' + str(myFile).split('.')[1] # 'RESUMES/' +
    print(file_name)
    print(os.getcwd())
    if os.getcwd() == 'D:\django_test':
        os.chdir('./Myapp/RESUMES')
        print(os.getcwd())
    os.system(r"touch {}".format(random_str + '.' + str(myFile).split('.')[1]))
    fp = open(file_name, 'wb+')
    print(fp)
    for i in myFile.chunks():
        print(i)
        fp.write(i)
    fp.close()
    return HttpResponse('')

def jiexi_resume(request):
    data = {}
    for i in request.POST.lists():
        print(i)
        data[i[0]] = i[1][0]
    print(data)
    #     datas = json.loads(i[0])
    # print(datas['random_str'])
    # HttpResponse('正在处理您的ID为：',datas['random_str'] )
    return HttpResponse('正在处理您的ID为：'+ data['random_str'],content_type='text/plain;charset=utf-8')

def upload_file(request):
    random_str = '123'
    myFile = request.FILES.get('file')
    print(myFile)
    file_name = random_str + '.' + str(myFile).split('.')[1]  # 'RESUMES/' +
    print(file_name)
    print(os.getcwd())
    if os.getcwd() == 'D:\django_test':
        os.chdir('./Myapp/RESUMES')
        print(os.getcwd())
    os.system(r"touch {}".format(random_str + '.' + str(myFile).split('.')[1]))
    fp = open(file_name, 'wb+')
    print(fp)
    for i in myFile.chunks():
        print(i)
        fp.write(i)
    fp.close()
    return HttpResponse('')